package org.htmlparser.parserapplications.filterbuilder.wrappers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.htmlparser.Attribute;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.parserapplications.filterbuilder.Filter;
import org.htmlparser.tags.CompositeTag;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class HasAttributeFilterWrapper extends Filter
  implements ActionListener, DocumentListener
{
  protected JComboBox mAttributeName;
  protected JTextArea mAttributeValue;
  protected HasAttributeFilter mFilter = new HasAttributeFilter();
  protected JCheckBox mValued;

  public HasAttributeFilterWrapper()
  {
    this.mAttributeName = new JComboBox();
    this.mAttributeName.setEditable(true);
    add(this.mAttributeName);
    this.mAttributeName.addItem(this.mFilter.getAttributeName());
    this.mAttributeName.addActionListener(this);
    String str = this.mFilter.getAttributeValue();
    this.mValued = new JCheckBox("Has Value");
    add(this.mValued);
    JCheckBox localJCheckBox = this.mValued;
    if (str != null)
    {
      bool = true;
      localJCheckBox.setSelected(bool);
      this.mValued.addActionListener(this);
      this.mAttributeValue = new JTextArea(2, 20);
      this.mAttributeValue.setBorder(new BevelBorder(1));
      add(this.mAttributeValue);
      if (str == null)
        break label193;
      this.mAttributeValue.setText(str);
    }
    while (true)
    {
      while (true)
      {
        this.mAttributeValue.getDocument().addDocumentListener(this);
        return;
        bool = false;
      }
      label193: this.mAttributeValue.setVisible(false);
    }
  }

  public boolean accept(Node paramNode)
  {
    return this.mFilter.accept(paramNode);
  }

  public void actionPerformed(ActionEvent paramActionEvent)
  {
    Object localObject = paramActionEvent.getSource();
    if (localObject == this.mAttributeName)
    {
      Object[] arrayOfObject = this.mAttributeName.getSelectedObjects();
      if ((arrayOfObject != null) && (arrayOfObject.length != 0))
        this.mFilter.setAttributeName((String)arrayOfObject[0]);
    }
    while (true)
    {
      while (true)
      {
        do
          return;
        while (localObject != this.mValued);
        if (!(this.mValued.isSelected()))
          break;
        this.mFilter.setAttributeValue(this.mAttributeValue.getText());
        this.mAttributeValue.setVisible(true);
      }
      this.mAttributeValue.setVisible(false);
      this.mAttributeValue.setText("");
      this.mFilter.setAttributeValue(null);
    }
  }

  protected void addAttributeValues(Set paramSet, Node paramNode)
  {
    if (paramNode instanceof Tag)
    {
      Vector localVector = ((Tag)paramNode).getAttributesEx();
      for (int i = 1; i < localVector.size(); ++i)
      {
        Attribute localAttribute = (Attribute)localVector.elementAt(i);
        if (localAttribute.getName() != null)
        {
          String str = localAttribute.getValue();
          if (str != null)
            paramSet.add(str);
        }
      }
      if (paramNode instanceof CompositeTag)
      {
        NodeList localNodeList = ((CompositeTag)paramNode).getChildren();
        if (localNodeList != null)
          for (int j = 0; j < localNodeList.size(); ++j)
            addAttributeValues(paramSet, localNodeList.elementAt(j));
      }
    }
  }

  protected void addAttributes(Set paramSet, Node paramNode)
  {
    if (paramNode instanceof Tag)
    {
      Vector localVector = ((Tag)paramNode).getAttributesEx();
      for (int i = 1; i < localVector.size(); ++i)
      {
        String str = ((Attribute)localVector.elementAt(i)).getName();
        if (str != null)
          paramSet.add(str);
      }
      if (paramNode instanceof CompositeTag)
      {
        NodeList localNodeList = ((CompositeTag)paramNode).getChildren();
        if (localNodeList != null)
          for (int j = 0; j < localNodeList.size(); ++j)
            addAttributes(paramSet, localNodeList.elementAt(j));
      }
    }
  }

  public void changedUpdate(DocumentEvent paramDocumentEvent)
  {
  }

  public String getDescription()
  {
    return "Has attribute";
  }

  public String getIconSpec()
  {
    return "images/HasAttributeFilter.gif";
  }

  public NodeFilter getNodeFilter()
  {
    HasAttributeFilter localHasAttributeFilter = new HasAttributeFilter();
    localHasAttributeFilter.setAttributeName(this.mFilter.getAttributeName());
    localHasAttributeFilter.setAttributeValue(this.mFilter.getAttributeValue());
    return localHasAttributeFilter;
  }

  public NodeFilter[] getSubNodeFilters()
  {
    return new NodeFilter[0];
  }

  public void insertUpdate(DocumentEvent paramDocumentEvent)
  {
    Document localDocument = paramDocumentEvent.getDocument();
    try
    {
      this.mFilter.setAttributeValue(localDocument.getText(0, localDocument.getLength()));
      return;
    }
    catch (BadLocationException localBadLocationException)
    {
      localBadLocationException.printStackTrace();
    }
  }

  public void removeUpdate(DocumentEvent paramDocumentEvent)
  {
    Document localDocument = paramDocumentEvent.getDocument();
    try
    {
      this.mFilter.setAttributeValue(localDocument.getText(0, localDocument.getLength()));
      return;
    }
    catch (BadLocationException localBadLocationException)
    {
      localBadLocationException.printStackTrace();
    }
  }

  public void setNodeFilter(NodeFilter paramNodeFilter, Parser paramParser)
  {
    this.mFilter = ((HasAttributeFilter)paramNodeFilter);
    HashSet localHashSet = new HashSet();
    paramParser.reset();
    try
    {
      NodeIterator localNodeIterator = paramParser.elements();
      if (localNodeIterator.hasMoreNodes())
        addAttributes(localHashSet, localNodeIterator.nextNode());
    }
    catch (ParserException localParserException)
    {
      Iterator localIterator = localHashSet.iterator();
      while (localIterator.hasNext())
        this.mAttributeName.addItem(localIterator.next());
      String str1 = this.mFilter.getAttributeName();
      if (!(str1.equals("")))
        this.mAttributeName.setSelectedItem(str1);
      String str2 = this.mFilter.getAttributeValue();
      if (str2 != null)
      {
        this.mValued.setSelected(true);
        this.mAttributeValue.setVisible(true);
        this.mAttributeValue.setText(str2);
      }
      while (true)
      {
        return;
        this.mValued.setSelected(false);
        this.mAttributeValue.setVisible(false);
      }
    }
  }

  public void setSubNodeFilters(NodeFilter[] paramArrayOfNodeFilter)
  {
  }

  public String toJavaCode(StringBuffer paramStringBuffer, int[] paramArrayOfInt)
  {
    StringBuffer localStringBuffer = new StringBuffer().append("filter");
    int i = paramArrayOfInt[1];
    paramArrayOfInt[1] = (i + 1);
    String str = i;
    spaces(paramStringBuffer, paramArrayOfInt[0]);
    paramStringBuffer.append("HasAttributeFilter ");
    paramStringBuffer.append(str);
    paramStringBuffer.append(" = new HasAttributeFilter ();");
    newline(paramStringBuffer);
    spaces(paramStringBuffer, paramArrayOfInt[0]);
    paramStringBuffer.append(str);
    paramStringBuffer.append(".setAttributeName (\"");
    paramStringBuffer.append(this.mFilter.getAttributeName());
    paramStringBuffer.append("\");");
    newline(paramStringBuffer);
    if (this.mFilter.getAttributeValue() != null)
    {
      spaces(paramStringBuffer, paramArrayOfInt[0]);
      paramStringBuffer.append(str);
      paramStringBuffer.append(".setAttributeValue (\"");
      paramStringBuffer.append(this.mFilter.getAttributeValue());
      paramStringBuffer.append("\");");
      newline(paramStringBuffer);
    }
    return str;
  }
}